<?php

if(!defined('BASEPATH')) exit('No direct script access allowed');
class Mcom_model extends CI_Model
{
    private $DB1;
    private $DB2;
    function __construct(){
        parent::__construct();
        $this->DB1 = $this->load->database('default', TRUE);
       // $this->DB2 = $this->load->database('backup', TRUE);
    }
    
    /******************************SQL查询开始****************************************/
    /**
     * SQL查询
     * @return Object
     */
    function query_sql_object($sql, $data = NULL){
        return $this->_reRsult($this->_commonQuery($sql, $data));
    }
    /**
     * SQL查询
     * @return Array
     */
    function query_sql_array($sql, $data = NULL){
        return $this->_reResult_array($this->_commonQuery($sql, $data));
    }
    
    function _commonQuery($sql, $data=NULL){
        return $this->DB1->query($sql, $data);
    }
    /**
     * 简单的执行语句,返回True/False
     * @return Boolean
     */ 
     function query_run($sql){
       return $this->DB1->simple_query($sql);
     }
   /******************************SQL查询结束****************************************/  
   
   /******************************HQL查询开始****************************************/
    /**
     *  HQL查询
     * @return Object
     */
    function query_hql_object($table,$DESC=NULL){
        $this->_commomDesc($DESC);
        $query = $this->DB1->get($table);  
        return $this->_reRsult($query);
    }


    /**
     * HQL查询,返回数组
     * @return Array
     */
    function query_hql_array($table,$DESC=NULL){
        $this->_commomDesc($DESC);
        $query = $this->DB1->get($table);
        return $this->_reResult_array($query);
    }
    /**
     * 根据指定的$where返回对象
     * @return Object
     */
    function query_Bywhere($table,$where,$DESC=NULL){
        $this->_commomDesc($DESC);
        $query = $this->DB1->get_where($table, $where);
        return $this->_reRsult($query);
    }
    function query_ByWhere_Limit($table, $where,$limit = null, $offset = null,$DESC=NULL){
        $this->_commomDesc($DESC);
        $query = $this->DB1->get_where($table, $where,$limit,$offset);
        return $this->_reRsult($query);
    }
    //2012-9-9新增
     function query_where_field_limit($field,$where='',$table='',$DESC='',$top=6){
        $this->DB1->limit($top);
        return $this->query_field_Bywhere_object($field,$where,$table,$DESC);
     } 
     function query_field_limit($field,$table='',$DESC='',$top=10){
        $this->DB1->limit($top);
        return $this->query_field_object($field,$table,$DESC);
     } 
    /**
     * 查询有多少条数据
     */
    function query_count($table, $where = null){

        $query = $this->DB1->get_where($table, $where);
                
        if($query->num_rows() > 0){
            return $query->num_rows();
        }
        return 0;
    }
     /**
     * 根据$where语句查询出指定的字段
     */
     function query_field_Bywhere_object($field,$where=NULL,$table='',$DESC=null){
       $this->DB1->select($field);
       return $this->query_Bywhere($table,$where,$DESC); 
     } 
    /**
     * 根据$where条件查询返回指定的$fields字段
     * @return Array
     */ 
    function query_field_Bywhere_array($fields,$where,$table,$DESC=null){
       $this->DB1->select($fields);
       $this->_commomDesc($DESC);
       $query = $this->DB1->get_where($table, $where);
       return $this->_reResult_array($query);
    }

    /**
     * 查询指定的$fields字符,$field是字符串
     */
    function query_field_object($fields, $table,$DESC=null){
        $this->DB1->select($fields);
        $this->_commomDesc($DESC);
        $query = $this->DB1->get($table);  
        return $this->_reRsult($query);
    }
    /**
     * 根据ID获取返回一个对象
     */
    function query_one_object($table, $where,$DESC=null){
        $this->_commomDesc($DESC);
        $query = $this->DB1->get_where($table, $where);
        if($query->num_rows() > 0){
            return $query->row();
        }
        return array();
    }
    function query_one_object_f($field,$where,$table,$DESC=null){
       $this->DB1->select($field);
       return $this->query_one_object($table,$where,$DESC); 
    }

    /**
     *  2012-4-21:Produces: SELECT SUM(age) as age FROM members
     */
    function query_sum_object($table,$field,$where){
        if(is_string($field)){
            $this->DB1->select_sum($field);
        }else if(is_array($field)){
   
            foreach($field as $f){
                $this->DB1->select_sum($f);
            }
        }
        
        return $this->query_one_object($table,$where);
    }
    
    /**
     * 2012-8-14: SELECT zh,SUM(hjje) hjje FROM xzxx WHERE qs='201222016' GROUP BY zh
     */ 
    function query_field_groupby_obj($field,$where,$group,$table){
        $this->DB1->select($field);
        $this->DB1->group_by($group);
        return $this->query_Bywhere($table,$where);
    }
/******************************HQL查询****************************************/

/*****************************私有方法开始***********************************/
    /**
     *  默认根据ID倒序,eg:$DESC="px asc";
     */
    function _commomDesc($DESC=NULL){
        if(empty($DESC)){
            $this->DB1->order_by('id', 'desc');
        }else{
            $this->DB1->order_by($DESC);
        }
    }
    /**
     * 返回对象或一个空数组
     */ 
    function _reRsult($query){

        if($query->num_rows() > 0){
            return $query->result();
        }
        return array();
    }
    /**
     * 如果存在返回1,否则返回0,一般用于登录或条件判断数据库是否存在此数据。
     */ 
    function _reBool($query){
        if($query->num_rows() > 0){
            return 1;
        }
        return 0 ;
    }
    /**
     * 返回数组或一个空数组
     */ 
    function _reResult_array($query){
        if($query->num_rows() > 0){
            return $query->result_array();
        }
        return array();
    }
/****************************私有方法结束***************************************/

/*****************************CRUD开始******************************************/
  /**
     * 增加记录
     * 参数：data或对象
     */
    function insert_data($table, $data=null){
        $this->DB1->insert($table,$data);
        //$this->DB2->insert($table,$data);
        $this->DB1->cache_delete_all();//清除所有缓存
        return $this->DB1->affected_rows();
    }
    function insert_data_simple($table,$in_datas=null){
        $this->DB1->insert($table,$in_datas);
    }
    function clear_cache(){
        $this->DB1->cache_delete_all();//清除所有缓存
    }
    /**
     * 增加记录,同时返回记录ID
     */
    function insert_data_reId($table, $data){
        $this->DB1->insert($table, $data);
        //$this->DB2->insert($table,$data);
        $this->DB1->cache_delete_all();//清除所有缓存
        return $this->DB1->insert_id();
    }
    //2012-8-11增加,用于处理 update je=je-xx
    function insert_data_set($table,$field,$value){
        $this->DB1->set($field,$field, FALSE);
        $this->DB1->insert($table); 
    }
    function update_data_where($table,$set,$where){
         $this->DB1->update($table,$set,$where);
         //$this->DB2->update($table,$set,$where);
         $this->DB1->cache_delete_all();//清除所有缓存
         return $this->DB1->affected_rows();
    }
    //2012-8-11增加,用于处理 update je=je-xx
    function update_data_set($table,$field,$value,$where){
        $this->DB1->set($field,$value, FALSE);
        $this->DB1->where($where);
        $this->DB1->update($table); 
    }
    function delete_data($table, $where){
         $this->DB1->delete($table, $where);
         //$this->DB2->delete($table, $where);
         $this->DB1->cache_delete_all();//清除所有缓存
         
    }
    /**
     *   批量删除记录
     *   参数:$data=>数组或对象数组
     */
    function deleteAll_data($table, $data = null){
        foreach ($data as $row){
            $this->deleteObj($table, $row->id);
        }
    }
    /**
     * TRUNCATE
     */ 
    function truncate_table($table){
        $this->DB1->truncate($table); 
    }
    function table_exists($nm){
    
        echo $this->DB1->table_exists("$nm");
    }
    

/*****************************CRUD结束******************************************/
/*****************************其它开始******************************************/
     /**
      *从基础数据表里取出指定name的数据 
      */ 
     function getFlByName($name,$DESC='px asc')  {
        $data=array('fl'=>$name);
        return $this->query_Bywhere(myMsg::XT_JCSJB,$data,$DESC);       
    }

    /**
     * 检索数据是否存在
     */          
    function checkIsExit($table,$where){
        $query = $this->DB1->get_where($table, $where);
        return $this->_reBool($query);
    }

    /**
     * 调用存储过程,取指定的编号 
     */ 
    function get_new_bh($bm = NULL){
        if ($bm != NULL){
            $sql = "CALL sp_getbh('$bm', @bh)";
            mysql_query($sql);
            $result = mysql_query('select @bh');
            $array = mysql_fetch_array($result);
            $bh = $array['@bh'];
            return $bh;
        }else{
            return FALSE;
        }    
    }
/*****************************其它结束******************************************/




/********************************共同检查字段开始*********************/

    
    /**
     * 返回Default='显示' 的字段，用于检索查询
     */
    function queryByTableReDefaultField($table='')
    {
        $sql='SHOW  FULL FIELDS FROM '.$table.' WHERE `Default`="display"';

        $all_arr = $this->query_sql_array($sql);
        $datas=array();
        foreach($all_arr as $row){
                $data = array();
                $data['field'] = $row['Field'];
                $data['comment'] = $row['Comment'];
                $datas[]=$data;
        }
       
        return $datas;
    }
      /**
     * 返回所有表字段,模板处使用
     */ 
     function query_all_tables(){
        $sql='SHOW TABLE STATUS';

        $all_arr = $this->query_sql_array($sql);
        $datas=array();
        foreach($all_arr as $row){
                $data = array();
                $data['name'] = $row['Name'];
                $data['comment'] = $row['Comment'];
                $datas[]=$data;
        }
        return $datas;
     }
     /**
     * 返回Comment字段 模板处使用
     */
    function query_common_field_table($table)
    {
        $sql="SHOW  FULL FIELDS FROM $table";
        
        $all_arr = $this->query_sql_array($sql);
        $datas=array();
        foreach($all_arr as $row){
            $field=$row['Field'];
            $commond=$row['Comment'];
            
            if(!$this->_filter_field($field) &&!empty($commond)){//如果不在过滤范围且注释不为空,则显示出来
                $data = array();
                $data['field'] = $field;
                $data['type']=$row["Type"];//类型
                $data['comment'] =$commond;
                $datas[]=$data;               
            }

        }
        return $datas;
    }
        //2012-7-03:过滤指共同不显示的字段
    function _filter_field($field){
        $filer = array("id", "lururiqi", "lururen", "xiugairiqi","xiugairen");
        if (in_array("$field", $filer)) {
           return true;
        }
        return false;
    }
/********************************共同检查字段结束*********************/



}
